package com.sdry.service.impl.zc;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sdry.mapper.zc.ZcUpAndDownMapper;
import com.sdry.model.zc.ZcTrayAndLocationEntity;
import com.sdry.service.zc.ZcUpAndDownService;

/**
 * 上下架管理
 * @ClassName:    ZcUpAndDownServiceImpl   
 * @Description:  
 * @Author:       zc   
 * @CreateDate:   2019年6月2日 下午1:36:00   
 * @Version:      v1.0
 */
@Service
public class ZcUpAndDownServiceImpl implements ZcUpAndDownService {

	@Resource
	private ZcUpAndDownMapper zcUpAndDownMapper;
	/**
	 * 上架
	 */
	@Override
	public int bindingTrayAndLocation(ZcTrayAndLocationEntity zcTrayAndLocationEntity) {
		String[] trayCodeList = zcTrayAndLocationEntity.getTray_code().split(",");
		for (String trayCode : trayCodeList) {
			//上架前查询物料id
			if(zcUpAndDownMapper.selectMid(trayCode) == null){
				return -2;	
			}else{
				zcTrayAndLocationEntity.setMid(zcUpAndDownMapper.selectMid(trayCode));
			}
		}
		int affact =0;
		int count = zcUpAndDownMapper.countTrayAndLocation(zcTrayAndLocationEntity);
		if(count == 0){
			//库位为空新增
			affact = zcUpAndDownMapper.bindingTrayAndLocation(zcTrayAndLocationEntity);
		}else{
			//库位为空追加
			affact = zcUpAndDownMapper.addTrayAndLocation(zcTrayAndLocationEntity);
		}
		return affact;
	}
	/**
	 * 下架
	 */
	@Override
	public int unbindTrayAndLocation(ZcTrayAndLocationEntity zcTrayAndLocationEntity) {
		ZcTrayAndLocationEntity zcTrayAndLocationEntityOld = zcUpAndDownMapper.selectTrayCodeByLocationCode(zcTrayAndLocationEntity);
		String[] tray_code_old = zcTrayAndLocationEntityOld.getTray_code().split(",");
		String[] tray_code_new = zcTrayAndLocationEntity.getTray_code().split(",");
		String new_tray_code_list="";
		for(int i = 0; i < tray_code_old.length; i++){
			for(int j = 0; j < tray_code_new.length; j++){
				if(tray_code_new[j].equals(tray_code_old[i])){
					tray_code_old = org.apache.commons.lang3.ArrayUtils.remove(tray_code_old, i);
				}
			}
		}
		for (String string : tray_code_old) {
			new_tray_code_list += string+",";
		}
		zcTrayAndLocationEntity.setNew_tray_code_list(new_tray_code_list);
		return zcUpAndDownMapper.unbindTrayAndLocation(zcTrayAndLocationEntity);
	}

}
